package org.light.framework.util;

import java.util.Enumeration;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

/**
 * Web相关工具类
 * @author de.cel
 *
 */
public class WebHelper {

	/**
	 * 获取当前请求表单中的值
	 * 
	 * @param request
	 * @return
	 */
	public static HashMap<String, Object> getFormDatas(HttpServletRequest request) {

		HashMap<String, Object> datas = new HashMap<String, Object>();

		Enumeration<String> names = request.getParameterNames();

		for (Enumeration<String> e = names; e.hasMoreElements();) {
			String name = e.nextElement().toString();
			String value = request.getParameter(name);
			datas.put(name, value);
		}
		return datas;
	}

	/**
	 * 获取表单中的值（通过ThreadLocal获取request）
	 * 
	 * @return
	 */
	public static HashMap<String, Object> getFormDatas() {

		ThreadLocal<HttpServletRequest> t = new ThreadLocal<HttpServletRequest>();
		if (t != null) {
			HttpServletRequest request = t.get();
			if (request != null) {
				return getFormDatas(request);
			}
		}
		return null;
	}

}
